<?php
/**
 * 测试WebSocket推送
 * 模拟刷脸签到，向WebSocket服务器推送消息
 * 
 * 使用方法：
 * php test_websocket_push.php [类型]
 * 
 * 类型：
 * - success: 签到成功
 * - end: 结束签到
 * - duplicate: 重复刷脸
 */

// 获取命令行参数
$type = $argv[1] ?? 'success';

// 验证类型
$validTypes = ['success', 'end', 'duplicate'];
if (!in_array($type, $validTypes)) {
    echo "错误：无效的类型 '{$type}'\n";
    echo "有效类型：" . implode(', ', $validTypes) . "\n";
    exit(1);
}

// 构建测试消息
$messages = [
    'success' => [
        'type' => 'voice_play',
        'voice_type' => 'success',
        'voice_file' => 'success.mp3',
        'time' => date('Y-m-d H:i:s'),
        'data' => [
            'member_id' => 123,
            'member_name' => '张三',
            'card_no' => '1542415',
            'action' => 'start',
            'record_id' => 456,
            'start_time' => date('Y-m-d H:i:s')
        ]
    ],
    'end' => [
        'type' => 'voice_play',
        'voice_type' => 'end',
        'voice_file' => 'end.mp3',
        'time' => date('Y-m-d H:i:s'),
        'data' => [
            'member_id' => 123,
            'member_name' => '张三',
            'card_no' => '1542415',
            'action' => 'end',
            'record_id' => 456,
            'duration_minutes' => 120,
            'points' => 12,
            'start_time' => date('Y-m-d H:i:s', strtotime('-2 hours')),
            'end_time' => date('Y-m-d H:i:s')
        ]
    ],
    'duplicate' => [
        'type' => 'voice_play',
        'voice_type' => 'duplicate',
        'voice_file' => 'duplicate.mp3',
        'time' => date('Y-m-d H:i:s'),
        'data' => [
            'member_id' => 123,
            'member_name' => '张三',
            'card_no' => '1542415',
            'interval' => 15
        ]
    ]
];

$message = $messages[$type];

echo "========================================\n";
echo "  测试WebSocket推送\n";
echo "========================================\n";
echo "类型: {$type}\n";
echo "消息: " . json_encode($message, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
echo "========================================\n\n";

// 连接到WebSocket服务器的内部通信端口
echo "正在连接到 127.0.0.1:8780...\n";

$socket = @stream_socket_client('tcp://127.0.0.1:8780', $errno, $errstr, 5);

if (!$socket) {
    echo "❌ 连接失败: {$errstr} ({$errno})\n";
    echo "\n提示：\n";
    echo "1. 请确保WebSocket服务器已启动\n";
    echo "2. 运行命令：php websocket_server.php start\n";
    exit(1);
}

echo "✓ 连接成功\n\n";

// 发送消息（text协议，以换行符结尾）
$jsonData = json_encode($message, JSON_UNESCAPED_UNICODE) . "\n";
fwrite($socket, $jsonData);

echo "✓ 消息已发送\n";
echo "✓ 字节数: " . strlen($jsonData) . "\n\n";

fclose($socket);

echo "========================================\n";
echo "  测试完成！\n";
echo "========================================\n";
echo "\n";
echo "请检查：\n";
echo "1. WebSocket服务器日志 - 应该看到 '收到内部消息'\n";
echo "2. 浏览器页面 - 应该看到新的签到记录\n";
echo "3. 浏览器音频 - 应该听到语音播报\n";
echo "\n";
echo "其他测试命令：\n";
echo "  php test_websocket_push.php success    # 测试签到成功\n";
echo "  php test_websocket_push.php end        # 测试结束签到\n";
echo "  php test_websocket_push.php duplicate  # 测试重复刷脸\n";
echo "\n";

